package top.drinyeit.weilong.presenter;

import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.Map;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.activity.UploadIdcardActivity;
import top.drinyeit.weilong.activity.UploadLicenseActivity;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.base.BaseActivity;
import top.drinyeit.weilong.model.AuthenticationDetailBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.SPUtil;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/8.
 */

public class EditCompanyPresenter {
    private BaseActivity activity;

    public EditCompanyPresenter(BaseActivity activity) {
        this.activity = activity;
    }

    //获取企业认证详情
    public void getCompany() {
        String user_id = SPUtil.getShareString(Constant.USER_ID);
        activity.showLoading();
        CommonSubscriber<HttpResponse<AuthenticationDetailBean>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<AuthenticationDetailBean>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<AuthenticationDetailBean> o) {
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    if (activity instanceof UploadIdcardActivity) {
                        if (o.getData().getId_card_back_url() != null) {
                            ((UploadIdcardActivity) activity).behindImg.setVisibility(View.VISIBLE);
                            Glide.with(activity).load(o.getData().getId_card_back_url())
                                    .placeholder(R.drawable.ic_def)
                                    .dontTransform().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                                    .into(((UploadIdcardActivity) activity).behindImg);
                            ((UploadIdcardActivity) activity).behindText.setVisibility(View.GONE);
                        }
                        if (o.getData().getId_card_front_url() != null) {
                            ((UploadIdcardActivity) activity).frontImg.setVisibility(View.VISIBLE);
                            Glide.with(activity).load(o.getData().getId_card_front_url())
                                    .placeholder(R.drawable.ic_def)
                                    .dontTransform().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                                    .into(((UploadIdcardActivity) activity).frontImg);
                            ((UploadIdcardActivity) activity).frontText.setVisibility(View.GONE);
                        }
                    }

                    if (activity instanceof UploadLicenseActivity){
                        if (o.getData().getLicense_picture_url() != null){
                            ((UploadLicenseActivity) activity).licenseImg.setVisibility(View.VISIBLE);
                            Glide.with(activity).load(o.getData().getLicense_picture_url()).placeholder(R.drawable.ic_def)
                                    .dontTransform().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                                    .placeholder(R.drawable.ic_def)
                                    .into(((UploadLicenseActivity) activity).licenseImg);
                            ((UploadLicenseActivity) activity).licenseText.setVisibility(View.GONE);
                        }
                    }
                } else {
                    activity.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().getCompany(subscriber, user_id);
    }

    //修改认证信息
    public void editCompany(Map<String, Object> map) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                activity.dismissLoading();
                activity.showToastMsg(o.getMessage());
                if (o.getCode() == COMMON_SUCCESS) {
                    activity.setResult(1);
                    activity.finish();
                }
            }
        });

        RetrofitMethod.getInstance().editCompany(subscriber, map);
    }

}
